package GUI;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class SimpleSearchTree extends JFrame {
  JPanel findPanel;
  JTextField findField;
  JTree tree;
  JButton findButton;
  DefaultMutableTreeNode[] nodes;

  public SimpleSearchTree() {
    String[] treelabels =  { "All Auctions","Closed Auction","Open Auctions" };
    Integer[] closedItems = { new Integer(500144),new Integer(500146),new Integer(500147) };
    Integer[] openItems ={ new Integer(500148),new Integer(500149)};

    nodes = new DefaultMutableTreeNode[treelabels.length];
    DefaultMutableTreeNode[] closednodes = new DefaultMutableTreeNode[closedItems.length];
    DefaultMutableTreeNode[] opennodes = new DefaultMutableTreeNode[openItems.length];
    for (int i=0; i < treelabels.length; i++) {
       nodes[i] = new
         DefaultMutableTreeNode(treelabels[i]);
    }
    nodes[0].add(nodes[1]);
    nodes[0].add(nodes[2]);

    for (int i=0; i < closedItems.length; i++) {
       closednodes[i] = new
		DefaultMutableTreeNode(closedItems[i]);
       nodes[1].add(closednodes[i]);
    }

    for (int i=0; i < openItems.length; i++) {
       opennodes[i] = new DefaultMutableTreeNode(
                            openItems[i]);
       nodes[2].add(opennodes[i]);
    }

    DefaultTreeModel model=new
                       DefaultTreeModel(nodes[0]);
    tree = new JTree(model);

    JScrollPane scroll = new JScrollPane(tree);
    getContentPane().add(scroll, BorderLayout.CENTER);
    findPanel= new JPanel();
    findField= new JTextField(10);
    findButton= new JButton("find");
    findButton.addActionListener (new ActionListener() {
      public void actionPerformed (ActionEvent e) {
        String field=findField.getText();
        if (field != null) {
          findNode(findField.getText());
        } else {
          return;
        }
      }
    });
    findPanel.add(findField);
    findPanel.add(findButton);
    getContentPane().add(findPanel, BorderLayout.SOUTH);
  }
  public void findNode(String field) {
    Enumeration e = nodes[0].depthFirstEnumeration();
    Object currNode;
    while (e.hasMoreElements()) {
      currNode = e.nextElement();
      if (currNode.toString().equals(field)) {
        TreePath path=new TreePath(((
	     DefaultMutableTreeNode)currNode).getPath());
        tree.makeVisible(path);
        tree.setSelectionRow(tree.getRowForPath(path));
        return;
      }
    }
  }

  public static void main(String[] args) {
    SimpleSearchTree frame = new SimpleSearchTree();
    /*frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.setVisible(true);*/
    frame.pack();
    frame.setSize(300,150);
  }
}
